package com.unicom.mec.user.controller;


import com.unicom.mec.user.common.ResultBean;
import com.unicom.mec.user.service.TenantService;
import com.unicom.mec.user.vo.TenantVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 租户控制层
 *
 * @author 刘亮武
 * @date 2020年06月11日
 */
@Api(value = "/tenant", tags = "租户管理")
@RestController
@RequestMapping("/tenant")
public class TenantController {

    @Autowired
    private TenantService tenantService;

    @ApiOperation(value = "根据uniqekuye获取租户主键",hidden = true)
    @GetMapping("/findTenantIdByUniqueKey/{uniqueKey}")
    @ResponseBody
    ResultBean<Long> findTenantIdByUniqueKey(@PathVariable("uniqueKey") String uniqueKey){
        Long tenantId = tenantService.findTenantIdByUniqueKey(uniqueKey);
        ResultBean<Long> result = new ResultBean<>();
        result.setData(tenantId);
        return result;
    }

    @ApiOperation(value = "根据主键获取信息",hidden = true)
    @GetMapping("/findTenantById/{id}")
    @ResponseBody
    ResultBean<TenantVo> findTenantById(@PathVariable("id") Long id){
        TenantVo tenantVo = tenantService.findTenantById(id);
        ResultBean<TenantVo> result = new ResultBean<>();
        result.setData(tenantVo);
        return result;
    }

    @ApiOperation(value = "根据主键数组获取信息",hidden = true)
    @GetMapping("/findTenantByIds/{ids}")
    @ResponseBody
    ResultBean<List<TenantVo>> findTenantByIds(@PathVariable("ids") Long[] ids){
        List<TenantVo> tenantVos = tenantService.findTenantByIds(ids);
        ResultBean<List<TenantVo>> result = new ResultBean<>();
        result.setData(tenantVos);
        return result;
    }

    @ApiOperation(value = "查询租户信息", httpMethod = "GET")
    @GetMapping("/")
    @ResponseBody
    ResultBean<List<TenantVo>> findTenants(@ApiParam(value = "省编码") @RequestParam(value = "provinceCode", required = false) String provinceCode,
                                           @ApiParam(value = "市编码") @RequestParam(value = "cityCode", required = false) String cityCode,
                                           @ApiParam(value = "区编码") @RequestParam(value = "districtCode", required = false) String districtCode,
                                           @ApiParam(value = "街道编码") @RequestParam(value = "streetCode", required = false) String streetCode) {
        List<TenantVo> tenantVos = tenantService.findTenants(provinceCode, cityCode, districtCode, streetCode);
        ResultBean<List<TenantVo>> result = new ResultBean<>();
        result.setData(tenantVos);
        return result;
    }

    @PostMapping("/register")
    @ApiOperation(value = "注册租户")
    //************************************
    ResultBean<String> register(TenantVo tenantVo){
        String resultInfo = tenantService.register(tenantVo);
        ResultBean<String> result = new ResultBean<>();
        if (StringUtils.equals(resultInfo, ResultBean.SUCCESS)) {
            result.setReturnMsg("租户注册成功");
        }else {
            result.setReturnCode(ResultBean.FAIL);
            result.setReturnMsg("租户注册失败");
        }
        return result;
    }
}
